package com.d.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilDate {
    /**
     * 年月日时分秒(无下划线) yyyyMMddHHmmss
     */
    public static final String dtLong = "yyyyMMddHHmmss";

    /**
     * 完整时间 yyyy-MM-dd HH:mm:ss
     */
    public static final String simple = "yyyy-MM-dd HH:mm:ss";

    /**
     * 年月日(无下划线) yyyyMMdd
     */
    public static final String dtShort = "yyyyMMdd";

    public static final SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * <返回指定格式的日期字符串>
     * <功能详细描述>
     *
     * @param date
     * @param format
     * @return
     * @author
     * @date
     * @see [类、类#方法、类#成员]
     */
    public static String DateToString(Date date, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String DateToStringShort(Date date) {
        return shortSdf.format(date);
    }

    /**
     * <返回 yyyy-MM-dd HH:mm:ss格式的日期字符串>
     * <功能详细描述>
     *
     * @param date
     * @return
     * @author
     * @date
     * @see [类、类#方法、类#成员]
     */
    public static String DateToString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(simple);
        return dateFormat.format(date);
    }

    public static Date stringToDate(String source) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(simple);
        try {
            return dateFormat.parse(source);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Date stringToDate(String source, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(source);
        } catch (ParseException e) {
            return new Date();
        }
    }

    /**
     * d2大于d1 返回 true
     *
     * @param d1
     * @param d2
     * @return
     * @throws Exception
     */
    public static boolean getDistanceDays(Date d1, Date d2)
            throws Exception {
        d1 = shortSdf.parse(shortSdf.format(d1));
        d2 = shortSdf.parse(shortSdf.format(d2));
        long time1 = d1.getTime();
        long time2 = d2.getTime();
        if (time1 < time2) {
            return true;
        } else {
            return false;
        }
    }

    public static int getDistance(Date d1, Date d2)
            throws Exception {
        d1 = shortSdf.parse(shortSdf.format(d1));
        d2 = shortSdf.parse(shortSdf.format(d2));
        long time1 = d1.getTime();
        long time2 = d2.getTime();
        if (time1 == time2) {
            return 0;
        } else if (time1 > time2) {
            return -1;
        } else {
            return 1;
        }
    }

    public static int compare_date(Date DATE1, Date DATE2) {

        try {
            Date dt1 = longSdf.parse(longSdf.format(DATE1));
            Date dt2 = longSdf.parse(longSdf.format(DATE2));
            if (dt1.getTime() > dt2.getTime()) {
//                System.out.println("dt1 在dt2后");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
//                System.out.println("dt1在dt2前");
                return -1;
            } else {
                return 1;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static Date getCurrentDayStartTime1() {
        Date now = new Date();
        try {
            now = shortSdf.parse(shortSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    public static Date getCurrentDayEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    public static Date getCurrentDayStartTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(shortSdf.format(now) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    public static Date getDayTimeTransfer(Date date, String format) {

        Date date1 = null;
        try {
            date1 = longSdf.parse(shortSdf.format(date) + format);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date1;
    }
}
